package com.lightheart.sphr.xiaobinggan.kg.ui;

import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.blankj.utilcode.util.ToastUtils;
import com.lightheart.sphr.xiaobinggan.R;
import com.lightheart.sphr.xiaobinggan.base.BaseActivity;
import com.lightheart.sphr.xiaobinggan.fragment.FragmentOne;
import com.lightheart.sphr.xiaobinggan.fragment.FragmentThree;
import com.lightheart.sphr.xiaobinggan.fragment.FragmentTwo;
import com.lightheart.sphr.xiaobinggan.kg.event.Switch;
import com.lightheart.sphr.xiaobinggan.utils.tablayout.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

public class SideSlipActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, TabLayout.OnTabClickListener {

    @BindView(R.id.navigation_view)
    NavigationView mNavigationView;
    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.ll_layout)
    LinearLayout ll_layout;
    private long mExitTime;
    private Fragment mCurrentFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sideslip;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
//        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); //禁止侧滑滑动
        mNavigationView.setNavigationItemSelectedListener(this);
        ArrayList<TabLayout.Tab> tabs = new ArrayList<>(); //fragment添加的地方
        tabs.add(new TabLayout.Tab(R.drawable.selector_home, R.string.title_home, FragmentOne.class));
        tabs.add(new TabLayout.Tab(R.drawable.selector_contract, R.string.title_contract, FragmentTwo.class));
        tabs.add(new TabLayout.Tab(R.drawable.selector_my, R.string.title_my, FragmentThree.class));
        mTabLayout.setUpData(tabs, this);
        mTabLayout.setCurrentTab(0);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onTabClick(TabLayout.Tab tab) {
        try {
            Fragment tmpFragment = getSupportFragmentManager().findFragmentByTag(tab.targetFragmentClz.getSimpleName());
            FragmentManager supportFragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (tmpFragment == null) {
                tmpFragment = tab.targetFragmentClz.newInstance();
                transaction.add(R.id.mFragmentContainerLayout, tmpFragment, tab.targetFragmentClz.getSimpleName());
                if (mCurrentFragment != null) {
                    transaction.hide(mCurrentFragment);
                }
                transaction.commit();
            } else {
                transaction.show(tmpFragment);
                if (mCurrentFragment != null) {
                    transaction.hide(mCurrentFragment);
                }
                transaction.commit();
            }
            mCurrentFragment = tmpFragment;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        String title = (String) menuItem.getTitle();
        Toast.makeText(this, "点击了----- " + title, Toast.LENGTH_SHORT).show();
        switch (menuItem.getItemId()){
            case R.id.textView:
                Toast.makeText(this, "点击我啦" + title, Toast.LENGTH_SHORT).show();
                break;
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
//                ToastUtils.showShort( R.string.exit_system );
                ToastUtils.showLong("再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister( this );
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void isDrawerOpen(Switch iSwitch) {
        if(iSwitch.iSwitch){
            if (!mDrawerLayout.isDrawerOpen(ll_layout)) {
                mDrawerLayout.openDrawer(ll_layout);
            }else {
                mDrawerLayout.closeDrawer(ll_layout);
            }
        }
    }
}
